package com.flipdf.viewer.stamp;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;

public class TextStamp implements Stamp {

	private String text;
	private Font font;
	private Color color;
	private TYPE type;
	private Rectangle rect;

	public TextStamp(String text) {
		this(text, new Font("Arial", Font.BOLD, 12), Color.red);
	}
	
	public TextStamp(String text, Font font, Color color) {
		this.text = text;
		this.font = font;
		this.color = color;
		this.type = TYPE.text;
		calcRect();
	}
	
	public TYPE getType() {
		return this.type;
	}

	public String getText() {
		return this.text;
	}

	public void move(int x, int y) {
		this.rect.x += x;
		this.rect.y += y;
	}
	
	public boolean containPoint(Point p) {
		return this.rect.contains(p);
	}

	public void setColor(Color color) {
		this.color = color;
	}
	
	public Color getColor() {
		return color;
	}
	
	public Font getFont() {
		return font;
	}
	
	public Rectangle getRect() {
		return rect;
	}

	public void draw(Graphics2D g2) {
		g2.setFont(font);
		g2.setColor(color);
		g2.drawRect(rect.x, rect.y, rect.width, rect.height);
		g2.drawString(text, rect.x + font.getSize(), rect.y + font.getSize());
	}

	private void calcRect() {
		if (this.rect == null) {
			this.rect = new Rectangle();
			this.rect.x = 20;
			this.rect.y = 20;
			int fontsize = font.getSize();
			this.rect.height = fontsize;
			this.rect.width = text.length() * fontsize;
		} else {
			int fontsize = font.getSize();
			this.rect.height = fontsize;
			this.rect.width = text.length() * fontsize;
		}
	}

	public String getColumn() {
		return text;
	}
}
